package Controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPasswordField;

import Entity.Customer;
import Entity.User;



import GUI.ClientConnection.ClientConnectToServerGui;
import GUI.ManagerEmployee.ManagerEmployeeGUI;
import GUI.MarketingEmployee.AddContactCustomerPnl;
import GUI.MarketingEmployee.AddCustomerPnl;
import GUI.MarketingEmployee.MarketingEmployeeGUI;
import GUI.SalesEmployee.SalesEmployeeGUI;
import MainPage.LogIn;
import Message.Message;
import S2.client.S2Client;

import javax.swing.JPanel;

import com.mysql.jdbc.UpdatableResultSet;

import javax.swing.JButton;

public abstract class MainControler implements java.awt.event.ActionListener{
	private static final ActionListener MainControler = null;
	//private static final ActionListener MarketingEmpControler = null;
	public static  S2Client clientConection;
	static String serverAdd = null;
	static String port = "0000";
	static int portInt;
	//static S2Client client;
	static JFrame mainFram;
	static JPanel mainPanel ;
	static ClientConnectToServerGui cctsg;
	static int flag=0;
	static JButton btnConnecttoserver;
	private static MainPage.LogIn LogInPage; 
	private static GUI.ManagerEmployee.ManagerEmployeeGUI manager;
	private static AddContactCustomerPnl mEmpGui;
//	private static GUI.MarketingEmployee.AddCustomerPnl addCustomerPnl; 
	
	public static void main(String[] args) throws IOException, InterruptedException {
		
		//define the new frame.
		mainFram = new JFrame("Main Frame");
		mainFram.setSize(600, 400);
		mainFram.getContentPane().setLayout(null);
		
		cctsg = new ClientConnectToServerGui();
		cctsg.btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				port = cctsg.textPort.getText();
				serverAdd = cctsg.textServerAdd.getText();						
			}
		});
		
		mainPanel = cctsg.mainPan;
		mainPanel.setBounds(49, 34, 461, 260);
		mainFram.getContentPane().add(mainPanel);
		updateNewPanel(mainPanel);
		
		// connect to the server.
		try {
			while(serverAdd ==null){
				Thread.sleep(1000);
			}
			portInt = Integer.parseInt(port);
			getInstance();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		//open the login frame after the connection to the server.
		if(clientConection != null){
			LogInPage = new LogIn();
			mainFram = LogInPage;
			mainFram.getContentPane().add(LogInPage.panel);
			updateNewPanel(LogInPage.panel);
		}		
		//get the connection details from the login frame. and send it to the server.
	    LogInPage.addController(MainControler);
		LogInPage.btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				User us = new User(LogInPage.getUserTextField().getText(),LogInPage.getPasswordField().getText());
				Message msg = new Message(us,"UserLogIn");
				try {
					System.out.print("\n\n\n****");
					System.out.println(clientConection.getHost());
					System.out.println(clientConection.getPort());
					System.out.println(clientConection.getInetAddress());
					System.out.print("\n\n\n****");
					clientConection.sendToServer(msg);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
	}	
	
	public static S2Client getInstance() {
		if (clientConection == null)
			try {
				clientConection = new S2Client(serverAdd, portInt);
			} catch (IOException e) {
				System.out.println("MainController -> getInstance");
				e.printStackTrace();
			}
		return clientConection;
	}

	public static void updateNewPanel(JPanel panel ){
		mainFram.setVisible(true);
		mainFram.setSize(600, 400);
		mainFram.revalidate();
		mainFram.repaint();
	}
	
	public static void setRoleFram(Message msg) {
		User user = (User) msg.getObj();
		switch (user.getRole()) {
		case 1://marketing employee
			
			mainFram.setVisible(false);
			MarketingEmpControler marketingControoler = new MarketingEmpControler();
			MarketingEmployeeGUI marketingGui = new MarketingEmployeeGUI();
			marketingControoler.setView(marketingGui);
			marketingGui.AddCustomerPnl.addController(marketingControoler);
			marketingGui.addContactCustomerPnl.addController(marketingControoler);
			marketingGui.addProductPnl.addController(marketingControoler);
			break;
			
		case 2://Manager Employee
			
			mainFram.setVisible(false);
			ManagerEmpCotntroler managerControoler = new ManagerEmpCotntroler();
			ManagerEmployeeGUI managerGui = new ManagerEmployeeGUI();
			managerControoler.setView(managerGui);
			managerGui.newSalePnl.addController(managerControoler);
			managerGui.getSaleOperationPnl.addController(managerControoler);
			managerGui.addSaleTemplatePnl.addController(managerControoler);
			managerGui.customerReportPnl.addController(managerControoler);
			managerGui.responsesReportPnl.addController(managerControoler);
			break;
			
		case 3://sale employee
			
			mainFram.setVisible(false);
			SalesEmpControler salesControoler = new SalesEmpControler();
			SalesEmployeeGUI salesGui = new SalesEmployeeGUI();
			salesControoler.setView(salesGui);
			//salesGui.setDe.addController(salesControoler);
			salesGui.setOrderPnl.addController(salesControoler);
			
			
			break;
		case 4:
			
			break;
			
		default:
			System.out.print("Wrong User");
			break;
		}
		
	}
	
}
